﻿//CustomClass Template - Created by CodenameGamma
//Replace Layout with the CC name, 
//and WoWClass.Mage with the Class your Designing for.
//Created July, 3rd 2010
//For use with Honorbuddy
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Threading;
using System.IO;
using System.Drawing;
using Styx;
using Styx.Combat.CombatRoutine;
using Styx.Helpers;
using Styx.Logic;
using Styx.Logic.Combat;
using Styx.Logic.Pathing;
using Styx.WoWInternals;
using Styx.WoWInternals.WoWObjects;
using TreeSharp;

namespace TitanShield
{
    class Classname : CombatRoutine
    {
        public override sealed string Name { get { return "Titan Shield v2.0.1"; } }

        public override WoWClass Class { get { return WoWClass.Warrior; } }

        private static LocalPlayer Me { get { return ObjectManager.Me; } }



        #region Log
        private void slog(string format, params object[] args) //use for slogging
        {
            Logging.Write(format, args);
        }
        #endregion


        #region Initialize
        public override void Initialize()
        {
            Logging.Write(Color.White, "_______________________________________________");
            Logging.Write(Color.Crimson, "-----" + Name + " CC by Stree -----");
            Logging.Write(Color.Yellow, "---- 99% of the credit goes to Shaddar ----");
            Logging.Write(Color.Crimson, "------ +rep Shaddar if you like the Arms section! ----");
            Logging.Write(Color.White, "_______________________________________________");
        }
        #endregion

        #region Settings

        public override bool WantButton
        {
            get
            {
                return true;
            }
        }

        public override void OnButtonPress()
        {
            Logging.Write(Color.Aqua, "No configuration yet!");
        }
        #endregion

        #region Add Detection
        //Credit to CodeNameGamma for detectAdds code
        private List<WoWUnit> adds = new List<WoWUnit>();

        private List<WoWUnit> detectAdds()
        {
            if (Me.CurrentTarget != null)
            {
                List<WoWUnit> addList = ObjectManager.GetObjectsOfType<WoWUnit>(false).FindAll(unit =>
                            unit.Guid != Me.Guid &&
                            unit.Location.Distance(Me.CurrentTarget.Location) <= 10 &&
                            (unit.IsTargetingMyPartyMember || unit.IsTargetingMyRaidMember || unit.IsTargetingMeOrPet) &&
                            !unit.IsFriendly &&
                            !unit.IsPet &&
                            !Styx.Logic.Blacklist.Contains(unit.Guid));

                if (addList.Count > 1)
                {
                    Logging.Write(Color.Crimson, "Detected " + addList.Count.ToString() + " adds!");
                }
                return addList;
            }
            else
            {
                return null;
            }
        }
        private bool IsTargetBoss()
        {
            string UnitClassification = Lua.GetReturnValues("local classification = UnitClassification(\"target\"); return classification")[0];
            string UnitLevel = Lua.GetReturnValues("local level = UnitLevel(\"target\"); return level")[0];
            if (!Me.IsInRaid)
            {
                if (UnitClassification == "worldboss" ||
                   (Me.CurrentTarget.Level == 87 && Me.CurrentTarget.Elite) ||
                   (Me.CurrentTarget.Level == 88))
                    return true;

                else return false;
            }
            else
            {
                if (UnitLevel == "-1")
                    return true;

                else return false;
            }


        }
        #endregion

        #region CastSpell Method
        // Credit to Apoc for the below CastSpell code
        // Used for calling CastSpell in the Combat Rotation
        public bool CastSpell(string spellName)
        {
            if (SpellManager.CanCast(spellName))
            {
                SpellManager.Cast(spellName);
                // We managed to cast the spell, so return true, saying we were able to cast it.
                return true;
            }
            // Can't cast the spell right now, so return false.
            return false;
        }
        public bool CastBuff(string name)
        {
            if (!PlayerHasBuff(name) && SpellManager.CanBuff(name, Me))
            {
                SpellManager.Cast(name);
                slog(name);
                return true;
            }
            return false;
        }

        public bool PlayerHasBuff(string name)
        {
            return PlayerBuffTimeLeft(name) > 0;
        }

        public bool TargetHasDebuff(string name)
        {
            return TargetDebuffTimeLeft(name) != 0; // <0 means passive aura; >0 means active one
        }
        public double PlayerBuffTimeLeft(string name)
        {
            try
            {
                var lua = string.Format("local x=select(7, UnitBuff('player', \"{0}\", nil, 'PLAYER')); if x==nil then return 0 else return x-GetTime() end", Lua.Escape(name));
                var t = double.Parse(Lua.GetReturnValues(lua)[0]);
                return t;
            }
            catch
            {
                slog("Lua failed in AuraTimeLeft");
                return 999999;
            }
        }

        public double TargetDebuffTimeLeft(string name)
        {
            try
            {
                var lua = string.Format("local x=select(7, UnitDebuff(\"target\", \"{0}\", nil)); if x==nil then return 0 else return x-GetTime() end", Lua.Escape(name));
                var t = double.Parse(Lua.GetReturnValues(lua)[0]);
                return t;
            }
            catch
            {
                slog("Lua failed in TargetAuraTimeLeft");
                return 999999;
            }
        }

        public double TargetDebuffTimeLeftAll(string name)
        {
            try
            {
                var lua = string.Format("local x=select(7, UnitDebuff(\"target\", \"{0}\", nil)); if x==nil then return 0 else return x-GetTime() end", Lua.Escape(name));
                var t = double.Parse(Lua.GetReturnValues(lua)[0]);
                return t;
            }
            catch
            {
                slog("Lua failed in TargetAuraTimeLeft");
                return 999999;
            }
        }

        public double SpellCooldown(string name)
        {
            var lua = string.Format("local x,y=GetSpellCooldown(\"{0}\"); return x+y-GetTime()", Lua.Escape(name));
            try
            {
                return double.Parse(Lua.GetReturnValues(lua)[0]);
            }
            catch
            {
                slog("Lua failed in SpellCooldown: " + lua);
                return 99999;
            }
        }

        private bool NeedStackExecutioner()
        {
            return !Me.HasAura("Executioner") || Me.Auras["Executioner"].StackCount < 5 || Me.Auras["Executioner"].TimeLeft.Seconds <= 3;
        }

        private bool NeedStackSunder()
        {
            return !Me.CurrentTarget.HasAura("Sunder Armor") || Me.CurrentTarget.Auras["Sunder Armor"].StackCount < 3 || Me.CurrentTarget.Auras["Sunder Armor"].TimeLeft.Seconds <= 3;
        }
        #endregion

        #region Combat Buffs

        public override bool NeedCombatBuffs
        {
            get
            {
                //              BattleShout();
                return false;
            }
        }

        public override void CombatBuff()
        {

        }

        #endregion

        #region CombatStart


        private void AutoAttack()
        {
            if (!Me.IsAutoAttacking && Me.CurrentTarget.Distance <= 7d)
            {
                Lua.DoString("StartAttack()");
            }
        }
        #endregion

        #region Combat

        public override void Combat()
        {
            if (SpellManager.Spells.Keys.Contains("Last Stand"))
            {
                if (!PlayerHasBuff("Defensive Stance"))
                {
                    CastBuff("Defensive Stance");
                }
                if (Me.CurrentTarget != null && Me.CurrentTarget.IsAlive == true && Me.Mounted == false)
                {
                    if (!PlayerHasBuff("Commanding Shout") || Me.CurrentRage < 20)
                    {
                        if (CastBuff("Commanding Shout") == true)
                            Logging.Write(Color.Yellow, ">> Commanding Shout <<");
                    }
                    if (Me.HasAura("Victorious") && Me.HealthPercent < 80)
                    {
                        if (CastSpell("Victory Rush") == true)
                            Logging.Write(Color.FloralWhite, ">> Victory Rush <<");
                    }
                    if (!TargetHasDebuff("Rend") || TargetDebuffTimeLeft("Rend") < 3)
                    {
                        if (CastSpell("Rend") == true)
                            Logging.Write(Color.Aqua, ">> Rend <<");
                    }
                    if (Me.Fleeing || Me.RagePercent < 20)
                    {
                        if (CastSpell("Berserker Rage") == true)
                            Logging.Write(Color.Yellow, ">> Berserker Rage <<");
                    }
                    ////////////////////////
                    ////// Interrupts //////
                    ////////////////////////
                    //if (Me.CurrentTarget.IsCasting && Me.CanInterruptCurrentSpellCast)
                    //{
                    //    if (CastSpell("Pummel") == true)
                    //        Logging.Write(Color.FloralWhite, ">> Pummel! Stop casting you bitch! <<");
                    //}

                    //////////////////////////
                    ////// AoE Rotation //////
                    //////////////////////////
                    if (detectAdds().Count > 1)
                    {
                        if (Me.CurrentTarget.HasAura("Rend") && CastSpell("Thunder Clap") == true && Me.Location.Distance(Me.CurrentTarget.Location) <= 10)
                        {
                            if (CastSpell("Thunder Clap") == true)
                                Logging.Write(Color.LightBlue, ">> Thunder Clap <<");
                        }
                        if (CastSpell("Shockwave") == true && Me.Location.Distance(Me.CurrentTarget.Location) <= 10)
                        {
                            if (CastSpell("Shockwave") == true)
                                Logging.Write(Color.LightBlue, ">> Shockwave <<");
                        }
                        if (CastSpell("Cleave") == true && CastSpell("Shockwave") == false)
                        {
                            if (CastSpell("Cleave") == true)
                                Logging.Write(Color.LightBlue, ">> Cleave <<");
                        }
                        if (Me.HasAura("Sword and Board"))
                        {
                            if (CastSpell("Shield Slam") == true)
                                Logging.Write(Color.LightBlue, ">> Free Shield Slam <<");
                        }
                        else if (CastSpell("Shield Slam") == true)
                        {
                            if (CastSpell("Shield Slam") == true)
                                Logging.Write(Color.LightBlue, ">> Shield Slam! <<");
                        }
                        if (CastSpell("Revenge") == true)
                        {
                            if (CastSpell("Revenge") == true)
                                Logging.Write(Color.LightBlue, ">> Revenge <<");
                        }
                        if (CastSpell("Shield Block") == true)
                        {
                            if (CastSpell("Shield Block"))
                                Logging.Write(Color.LightPink, ">> Shield Block <<");
                        }
                    }
                    else
                    {
                        ////////////////////////////////////
                        ////// Single Target Rotation //////
                        ////////////////////////////////////
                        if (Me.HasAura("Sword and Board"))
                        {
                            if (CastSpell("Shield Slam") == true)
                                Logging.Write(Color.LightGreen, ">> Free Shield Slam <<");
                        }
                        else if (CastSpell("Shield Slam") == true)
                        {
                            if (CastSpell("Shield Slam") == true)
                                Logging.Write(Color.LightGreen, ">> Shield Slam! <<");
                        }
                        if (CastSpell("Revenge") == true)
                        {
                            if (CastSpell("Revenge") == true)
                                Logging.Write(Color.LightGreen, ">> Revenge <<");
                        }
                        else if (!TargetHasDebuff("Sunder Armor") || NeedStackSunder())
                        {
                            if (CastSpell("Devastate") == true)
                                Logging.Write(Color.Green, ">> Devastate <<");
                        }
                        if (Me.CurrentRage > 60 || Me.ActiveAuras.ContainsKey("Incite"))
                        {
                            if (CastSpell("Heroic Strike") == true)
                                Logging.Write(Color.Green, ">> Heroic Strike <<");
                        }
                        if (Me.CurrentTarget.HasAura("Rend") && !CastSpell("Shield Slam") && !CastSpell("Revenge") && Me.CurrentTarget.HasAura("Sunder Armor"))
                        {
                            if (CastSpell("Thunder Clap") == true)
                                Logging.Write(Color.LightGreen, ">> Thunder Clap <<");
                        }
                        if (CastSpell("Shield Block") == true)
                        {
                            if (CastSpell("Shield Block"))
                                Logging.Write(Color.Pink, ">> Shield Block <<");
                        }
                        if (Me.Combat && !Me.CurrentTarget.HasAura("Demoralizing Shout") || TargetDebuffTimeLeft("Demoralizing Shout") < 3)
                        {
                            if (CastSpell("Demoralizing Shout") == true)
                                Logging.Write(Color.Yellow, ">> Demoralizing Shout <<");
                        }
                        if (CastSpell("Shockwave") == true && Me.Location.Distance(Me.CurrentTarget.Location) <= 10)
                        {
                            if (CastSpell("Shockwave") == true)
                                Logging.Write(Color.LightGreen, ">> Shockwave <<");
                        }
                    }

                    ///////////////////////////
                    ////// Prot Cooldowns /////
                    ///////////////////////////

                    //if (Me.HealthPercent <= 40)
                    //  Lua.DoString("RunMacroText('/use 13');");

                    if (Me.HealthPercent <= 40)
                        Lua.DoString("RunMacroText('/use 14');");

                    if (Me.HealthPercent <= 20)
                        Lua.DoString("RunMacroText('/use Healthstone');");

                    //if (Me.HealthPercent <= 30 && IsTargetBoss())
                    //{
                    //    if (CastSpell("Shield Wall") == true)
                    //        Logging.Write(color.Pink, ">> Shield Wall - Low HP <<");
                    //}

                    if (Me.HealthPercent <= 30 && IsTargetBoss() && CastSpell("Shield Wall") == false)
                    {
                        if (CastSpell("Enraged Regeneration") == true)
                            Logging.Write(Color.Pink, ">> Enraged Regeneration - Low HP <<");
                    }

                    //if (Me.HealthPercent <= 20 && IsTargetBoss() && CastSpell("Shield Wall") == false)
                    //{
                    //    if (CastSpell("Rallying Cry") == true)
                    //        Logging.Write(color.Pink, ">> Rallying Cry - Low HP <<");
                    //}


                    ////////////////////////////////////////
                    ////// Out of Range rotation here //////
                    ////////////////////////////////////////

                    //if (Me.CurrentTarget.Distance >=12d && Me.CurrentTarget.Distance <=30d)
                    //{
                    //    if (CastSpell("Charge") == true)
                    //        Logging.Write(Color.Crimson, ">> Charge <<");
                    //}

                    //if (Me.CurrentTarget.Distance >=12d && Me.CurrentTarget.Distance <=40d && SpellManager.Spells["Charge"].CooldownTimeLeft.TotalSeconds < 10 && SpellManager.Spells["Charge"].CooldownTimeLeft.TotalSeconds > 1)
                    //{
                    //    Lua.DoString("CastSpellByName('Heroic Leap');");
                    //    LegacySpellManager.ClickRemoteLocation(StyxWoW.Me.CurrentTarget.Location); //Took freakin ages to get this code right
                    //}
                }
            }
            if (SpellManager.Spells.Keys.Contains("Deadly Calm"))
            {
                if (Me.CurrentTarget != null && Me.CurrentTarget.IsAlive == true && Me.Mounted == false)
                {

                    ///////////////////////////////////////////////Stance 'n Shouts////////////////////////////////////////////////////////////////////////////////////////////////
                    {
                        if (Me.CurrentTarget.HealthPercent > 20)
                            Lua.DoString("RunMacroText('/cast [stance:3/3, nomodifier]Battle Stance');");
                    }
                    {
                        if (detectAdds().Count < 2 && Me.CurrentTarget.HealthPercent <= 20)
                            Lua.DoString("RunMacroText('/cast [stance:1/3, nomodifier]Berserker Stance');");
                    }
                    {
                        if (Me.Combat && Me.RagePercent < 30)
                            if (CastSpell("Battle Shout") == true)
                                Logging.Write(Color.Red, ">> Battle Shout <<");
                    }
                    /////////////////////////////////////////////AoE Rotation here/////////////////////////////////////////////////////////////////////////////////////////////////
                    {
                        if (detectAdds().Count >= 3 && Me.Auras.ContainsKey("Deadly Calm") == false && Me.Auras.ContainsKey("Sweeping Strikes") == false)
                            if (CastSpell("Bladestorm") == true)
                                Logging.Write(Color.Aqua, ">> Bladestorm <<");
                    }
                    {
                        if (detectAdds().Count >= 2)
                            if (CastSpell("Sweeping Strikes") == true)
                                Logging.Write(Color.Aqua, ">> Sweeping Strikes <<");
                    }
                    {
                        if (detectAdds().Count > 3 && Me.Auras.ContainsKey("Sweeping Strikes") == false)
                            if (CastSpell("Inner Rage") == true && CastSpell("Cleave") == true)
                                Logging.Write(Color.Aqua, ">> Cleave <<");

                        /////////////////////////////////////////////Single Target DPS here////////////////////////////////////////////////////////////////////////////////////////////

                        if (Me.HasAura("Victorious") /*&& Me.HealthPercent < 35*/)
                        {
                            if (CastSpell("Victory Rush") == true)
                            {
                                Logging.Write(Color.FloralWhite, ">> Victory Rush <<");
                            }
                        }
                        if (!TargetHasDebuff("Rend"))
                        {
                            if (CastSpell("Rend") == true)
                            {
                                Logging.Write(Color.Aqua, ">> Rend <<");
                            }
                        }
                        if (Me.Auras.ContainsKey("Taste for Blood") == true && Me.CurrentTarget.HealthPercent > 20)
                        {
                            if (CastSpell("Overpower") == true)
                            {
                                Logging.Write(Color.Aqua, ">> Overpower <<");
                            }
                        }
                        if (Me.CurrentTarget.HealthPercent > 20)
                        {
                            if (CastSpell("Mortal Strike") == true)
                            {
                                Logging.Write(Color.Aqua, ">> Mortal Strike <<");
                            }
                        }
                        if (!TargetHasDebuff("Colossus Smash"))
                        {
                            if (CastSpell("Colossus Smash") == true)
                            {
                                Logging.Write(Color.Aqua, ">> Colossus Smash <<");
                            }
                        }
                        if (!PlayerHasBuff("Taste for Blood") && SpellManager.Spells["Mortal Strike"].CooldownTimeLeft.Milliseconds > 560
                            && Me.CurrentTarget.HealthPercent > 20)
                        {
                            if (CastSpell("Slam") == true)
                            {
                                Logging.Write(Color.Aqua, ">> Slam! <<");
                            }
                        }
                        if (Me.RagePercent > 70 && detectAdds().Count < 2 && Me.CurrentTarget.HealthPercent > 20)
                        {
                            if (CastSpell("Heroic Strike") == true)
                            {
                                Logging.Write(Color.Aqua, ">> Heroic Strike <<");
                            }
                        }

                        //////////////////////////////////////////////Execute Phase here////////////////////////////////////////////////////////////////////////////////////////////////  

                    }
                    {
                        if (Me.CurrentTarget.HealthPercent <= 20 && NeedStackExecutioner())
                            if (CastSpell("Execute") == true)
                                Logging.Write(Color.Red, ">> Execute! <<");
                    }
                    {
                        if (Me.CurrentTarget.HealthPercent <= 20 && !NeedStackExecutioner())
                            if (CastSpell("Mortal Strike") == true)
                                Logging.Write(Color.Red, ">> Mortal Strike! <<");
                    }
                    {
                        if (SpellManager.Spells["Mortal Strike"].CooldownTimeLeft.Milliseconds > 560 && Me.RagePercent >= 60 && Me.CurrentTarget.HealthPercent <= 20)
                            if (CastSpell("Execute") == true)
                                Logging.Write(Color.Red, ">> Execute! <<");
                    }



                    /////////////////////////////////////////////Cooldowns are here////////////////////////////////////////////////////////////////////////////////////////////////


                    {
                        if (Me.Auras.ContainsKey("Slaughter") && IsTargetBoss())
                            if (CastSpell("Recklessness") == true)
                                Logging.Write(Color.FloralWhite, ">> Recklessness <<");
                    }
                    {
                        if (Me.RagePercent < 20)
                            if (CastSpell("Berserker Rage") == true)
                                Logging.Write(Color.FloralWhite, ">> Berserker Rage <<");
                    }
                    {
                        if (Me.HasAura("Berserker Rage") && Me.HealthPercent < 30)
                            if (CastSpell("Enraged Regeneration") == true)
                                Logging.Write(Color.FloralWhite, ">> Enraged Regeneration <<");
                    }
                    {
                        if (Me.CurrentTarget.HasAura("Rend") == true && Me.CurrentTarget.Distance <= 7d)
                            if (CastSpell("Deadly Calm") == true)
                                Logging.Write(Color.Crimson, ">> Deadly Calm <<");
                    }
                    {
                        if (Me.RagePercent > 70)
                            if (CastSpell("Inner Rage") == true)
                                Logging.Write(Color.Crimson, ">> Inner Rage <<");
                    }
                    {
                        if (IsTargetBoss())
                            Lua.DoString("RunMacroText('/use 13');");
                    }
                    {
                        if (IsTargetBoss())
                            Lua.DoString("RunMacroText('/use 14');");
                    }
                    {
                        if (IsTargetBoss())
                            Lua.DoString("RunMacroText('/use 10');");

                        //////////////////////////////////////////////////Racial Skills here/////////////////////////////////////////////////////////////////////////////////////////
                    }
                    {
                        if (IsTargetBoss())
                            Lua.DoString("RunMacroText('/Cast Berserking');");
                    }
                    {
                        if (IsTargetBoss() && Me.HasAura("Recklessness"))
                            Lua.DoString("RunMacroText('/Cast Blood Fury');");


                        ////////////////////////////////////////
                        ////// Out of Range rotation here //////
                        ////////////////////////////////////////

                        //if (Me.CurrentTarget.Distance >=12d && Me.CurrentTarget.Distance <=30d)
                        //{
                        //    if (CastSpell("Charge") == true)
                        //        Logging.Write(Color.Crimson, ">> Charge <<");
                        //}

                        //if (Me.CurrentTarget.Distance >=12d && Me.CurrentTarget.Distance <=40d && SpellManager.Spells["Charge"].CooldownTimeLeft.TotalSeconds < 10 && SpellManager.Spells["Charge"].CooldownTimeLeft.TotalSeconds > 1)
                        //{
                        //    Lua.DoString("CastSpellByName('Heroic Leap');");
                        //    LegacySpellManager.ClickRemoteLocation(StyxWoW.Me.CurrentTarget.Location); //Took freakin ages to get this code right
                        //}

                    }
                }

            }

        #endregion

        }
    }
}